import { _decorator, animation, Animation } from "cc";
import { Entity } from "../Entity";
import { AnimatorParam } from "../../enums/AnimatorParam";
import { DirectionNames } from "../../enums/Direction";
import { CocosUtils } from "../../utils/CocosUtils";
const { ccclass } = _decorator;

@ccclass("DeathState")
export class DeathState extends animation.StateMachineComponent {

    public onMotionStateEnter(controller: animation.AnimationController, motionStateStatus: Readonly<animation.MotionStateStatus>): void {
        const entity = controller.getComponent(Entity);
        const entityName = entity.name;
        const animation = controller.getComponentInChildren(Animation);
        const state = controller.getValue(AnimatorParam.STATE);
        const deathType = state === Entity.State.DEATH ? "death" : "airdeath";
        const atlasPath = entity.type === Entity.Type.DOOR ? `texture/door/death/death` : `texture/${entityName}/${deathType}/${DirectionNames[entity.direction]}/death`
        const animName = `${entityName}/${deathType}/${DirectionNames[entity.direction]}`;
        CocosUtils.playAnimation(animation, animName, atlasPath);
        controller.setValue(AnimatorParam.STATE, -1);
    }
}